package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.UniversalDeviceAction;
import com.qiantu.api.entity.UniversalDeviceData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.widget.SectionSeekBar;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class UniversalConfigActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //不要修改，需要根据position
    protected int NORMAL_TYPE = 0;
    protected int TEMPERATURE_TYPE = 1;

    private List<UniversalDeviceData> actionDtos;

    public UniversalConfigActionAdapter(List<UniversalDeviceData> actionDtos) {
        this.actionDtos = actionDtos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TEMPERATURE_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.universal_config_temperature, parent, false);
            return new UniversalConfigActionAdapter.TemperatureItemViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.universal_action_item, parent, false);
            return new UniversalConfigActionAdapter.NormalItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        UniversalDeviceData actionDto = actionDtos.get(position);
        if(holder instanceof NormalItemViewHolder){
            UniversalConfigActionAdapter.NormalItemViewHolder vh = (UniversalConfigActionAdapter.NormalItemViewHolder) holder;
            vh.checkbox.setTag(actionDto);
            vh.checkbox.setOnClickListener(mCheckListener);
            vh.roomName.setText(actionDto.getRangeName());
            vh.checkbox.setText("");
            vh.checkbox.setChecked(actionDto.getEnable());
        }else if(holder instanceof TemperatureItemViewHolder){
            UniversalConfigActionAdapter.TemperatureItemViewHolder vh = (UniversalConfigActionAdapter.TemperatureItemViewHolder) holder;
            vh.roomName.setText(actionDto.getActionName());
            vh.sectionSeekBar.setMinMaxValue(16,32);
            vh.sectionSeekBar.setUnit("℃");
            vh.sectionSeekBar.setValue(actionDto.getMin(), actionDto.getMax());
            vh.sectionSeekBar.setOnSeekBarChangeListener(new SectionSeekBar.OnSeekBarChangeListener() {
                @Override
                public void onValueChanged(int beginValue, int endValue) {
                    actionDtos.get(position).setMin(beginValue);
                    actionDtos.get(position).setMax(endValue);
                }

                @Override
                public void onStartTrackingTouch() {

                }

                @Override
                public void onStopTrackingTouch(int beginValue, int endValue) {
                    actionDtos.get(position).setMin(beginValue);
                    actionDtos.get(position).setMax(endValue);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        UniversalDeviceData actionDto = actionDtos.get(position);
        if (actionDto.getActionIdentifier().equals("temperature")) {
            return TEMPERATURE_TYPE;
        } else {
            return NORMAL_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return actionDtos.size();
    }

    private class NormalItemViewHolder extends RecyclerView.ViewHolder {
        private TextView roomName;
        private CheckBox checkbox;

        private NormalItemViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            roomName = itemView.findViewById(R.id.room_name);
            checkbox = itemView.findViewById(R.id.checkbox);
        }
    }

    private View.OnClickListener mCheckListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UniversalDeviceData actionDto = (UniversalDeviceData) v.getTag();
                if (((CheckBox)v).isChecked()){
                    actionDto.setEnable(true);
                }else {
                    actionDto.setEnable(false);
                }
            }
    };

    private class TemperatureItemViewHolder extends RecyclerView.ViewHolder {
        private TextView roomName;
        private SectionSeekBar sectionSeekBar;

        private TemperatureItemViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            roomName = itemView.findViewById(R.id.room_name);
            sectionSeekBar = itemView.findViewById(R.id.section_seek_bar);
        }
    }
}
